package utils

import (
	"crypto/md5"
	"fmt"
	"time"
)

var config = map[string]string{
	"push_key":    "kqeXveEboF",                        //在域名管理里，访问控制，URL鉴权的key
	"live_key":    "nOoQagTYv6",                        //在域名管理里，访问控制，URL鉴权的key
	"push_domain": "rtmp://xfsongpush.golangblogs.com", //直播的推流域名
	"live_domain": "xfsonglive.golangblogs.com",        //播流域名
	"app_name":    "xfsong_live",                       //应用名称
	"stream_name": "0",                                 //流名称
	"expire":      "1800",                              //直播过期时间
}

// 获取推拉流地址
func GetLiveUrl(userId int, timestamp int) map[string]string {
	//定义返回结果集的映射关系
	res := make(map[string]string)
	//流名称，把整型专程字符串
	config["stream_name"] = fmt.Sprintf("%d", userId)

	//如果没有传开始直播时间，则就是当前时间
	if timestamp == 0 {
		timestamp = int(time.Now().Unix())
	}

	//直播过期时间默认30分钟
	timestamp += 1800

	pushSString := fmt.Sprintf("/%s/%s-%d-0-0-%s", config["app_name"], config["stream_name"], timestamp, config["push_key"])
	liveSString := fmt.Sprintf("/%s/%s.flv-%d-0-0-%s", config["app_name"], config["stream_name"], timestamp, config["live_key"])

	pushMD5 := md5.Sum([]byte(pushSString))
	liveMD5 := md5.Sum([]byte(liveSString))

	//推流地址，用来推流
	res["push_url"] = config["push_domain"] + "/" + config["app_name"]
	//推流key和推流搭配使用
	res["push_key"] = config["stream_name"] + "?auth_key=" + fmt.Sprintf("%d-0-0-%x", timestamp, pushMD5)

	//多种格式的播流域名，用flv多
	res["flv_live_url"] = "http://" + config["live_domain"] + "/" + config["app_name"] + "/" + config["stream_name"] + ".flv?auth_key=" + fmt.Sprintf("%d-0-0-%x", timestamp, liveMD5)

	res["rtmp_live_url"] = "rtmp://" + config["live_domain"] + "/" + config["app_name"] + "/" + config["stream_name"] + "?auth_key=" + fmt.Sprintf("%d-0-0-%x", timestamp, liveMD5)

	res["m3u8_live_url"] = "http://" + config["live_domain"] + "/" + config["app_name"] + "/" + config["stream_name"] + ".m3u8?auth_key=" + fmt.Sprintf("%d-0-0-%x", timestamp, liveMD5)

	return res
}
